<!--
@name:accountDialog
@author:Zhenglei
@date:14:20
@description:accountDialog
@update:2022/5/1114:20
-->
<template>
  <div>
    <el-dialog :visible.sync="visible"
               title="用户信息"
               width="30%"
               center
               :before-close="modalClose"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="submitData"
               ref="formModelRef"
               label-width="180px"
               :rules='formModelRules'>
        <el-form-item label="法人姓名：" prop="userName">
          <el-input class="w-300" v-model="submitData.userName" clearable
                    placeholder="请输入法人姓名"/>
        </el-form-item>
        <el-form-item label="账号：" prop="account">
          <el-input class="w-300" v-model="submitData.account" clearable
                    placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input class="w-300" v-model="submitData.password"
                    show-password
                    clearable
                    placeholder="请输入密码"/>
        </el-form-item>

      </el-form>
      <p align="center">
        <el-button @click="modalClose"
                   type="info">取 消
        </el-button>
        <el-button type="primary"
                   @click="handleSubmit()">确 认
        </el-button>
      </p>

    </el-dialog>
  </div>
</template>

<script>
import md5 from "js-md5";

export default {
  name: "accountDialog",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      submitData: {
        companyId: '',
        userName: '',
        account: '',
        password: '',
      },
      formModelRules: {
        account: [{required: true, message: '请输入账号', trigger: 'blur'}],
        userName: [{required: true, message: '请输入法人姓名', trigger: 'blur'}],
        password: [
          {required: true, type: 'string', message: '请输入密码', trigger: 'blur'},
          {type: 'string', min: 8, max: 20, message: '请输入8-16位数字、字母组成，区分大小写', trigger: 'blur'}],
      }
    }
  },
  created() {
    console.log(this.dataObj)
    this.submitData.companyId = this.dataObj.id
    this.submitData.userName = this.dataObj.legalName
    this.submitData.account = this.dataObj.account.split('-')[1]
    this.submitData.password = this.dataObj.password
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    handleSubmit() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          let parse = JSON.parse(JSON.stringify(this.submitData));
          parse.account = '+86-' + parse.account
          parse.password = md5(parse.account + parse.password)
          this.post(this.api.company.updateModify, parse).then((res) => {
            if (res.data.code === '200') {
              this.$message.success('修改成功')
              this.modalClose()
              this.$parent.initTableData()
            }
          })
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
